<?php

class Operation
{

	public function addAllBonusRukovod(&$o, $summ, $des_mavro_valuta = null, $des_mavro_type = null, $des_sverh = null)
	{
		$CI = &get_instance();
		$user_id = $o['user_id'];
		$i = 0;
		while($user_id && $i < 20)
		{
			$i++;
			$level = str_repeat('*', $i);
			$user_id = $CI->MUser->getDesyatnikId($user_id, $o['date']);
			if($user_id)
			{
				$mv = $level == '*' ? $des_mavro_valuta : null;
				$mt = $level == '*' ? $des_mavro_type : null;
				$s = $level == '*' ? $des_sverh : null;
				$this->addBonusRukovod($o, $summ, $level, $user_id, $o['id'], $mv, $mt, $s);
			}
		}
	}

	public function addBonusRukovod($o, $summ, $level, $user_ruk_id, $parent_id = 0, $mavro_valuta = null, $depo_type = null, $sverh = null)
	{
		global $USER_LEVEL_BONUS_TXT, $VALUTA;
		$CI = &get_instance();

		$mavro_valuta = $mavro_valuta ? $mavro_valuta : $o['valuta'];
		$depo_type = $depo_type ? $depo_type : 'base';
		if($depo_type == 'base')
		{
			$percent = 30;
		}
		else
		{
			$des_user = $CI->MUser->get($user_ruk_id);
			$percent = getPercentByType($depo_type, $sverh, $des_user, $o['date']);
		}
		$ref_kurs = $CI->MKurs->getKurs($o['date'], $percent, 'buy');

		$kurs = $CI->MKurs->getKurs($o['date'], $percent, 'buy');
		$user = $CI->MUser->get($o['user_id']);
		$bonus_ruk = $level == '*' ? 5 : ($level == '**' ? 3 : 1);

		$save = array(
			'parent_id' => $parent_id,
			'user_id' => $user_ruk_id,
			'date' => $o['date'],
			'type' => 'l'.$level,
			'bonus' => 1,
			'summ' => round($summ * $bonus_ruk / 100, 4),
			'valuta' => $o['valuta'],
			'kurs' => $kurs,
			'percent' => $percent,
			'mavro_valuta' => $mavro_valuta,
			'hranen' => 'бонус '.$USER_LEVEL_BONUS_TXT[$level].' '.$bonus_ruk.'% '.$user['fio'].' ('.$user['email'].')',
		);
		$save['kurs_valuta'] = $save['mavro_valuta'] == $save['valuta'] ? 1 : $CI->MKurs->getExchange($save['date'], $VALUTA[$save['valuta']], $VALUTA[$save['mavro_valuta']]);
		$save['kurs_valuta'] = $save['kurs_valuta'] > 0 ? $save['kurs_valuta'] : 1;
		$save['mavro'] = $save['summ'] * $save['kurs_valuta'] / $save['kurs'];
		$save['mavro'] = round($save['mavro'], 4);
		$save['ostatok'] = $save['mavro'];
		$id = 0;
		$CI->MOper->save($save, $id);
		return $id;
	}

	public function getAllChildren(&$oper, $check_modify = true)
	{
		$CI = &get_instance();
		$out = $CI->MOper->getList(array('parent_id' => $oper['id'], 'sort' => 'date asc,num asc'));
		if($check_modify)
		{
			$other_modify = $this->getOtherModify($oper);
			if(@$other_modify['id'])
			{
				$out = array_merge(array($other_modify['id'] => $other_modify), $out);
			}
		}
		foreach($out as $o)
		{
			$out = array_merge($out, $this->getAllChildren($o, false));
		}
		$mass_id = $CI->MOper->getMassToOperId($oper['id']);
		if($mass_id)
		{
			$mass = $CI->MOper->get($mass_id);
			$out[$mass['id']] = $mass;
		}
		return $out;
	}

	public function getOtherModify(&$oper)
	{
		$CI = &get_instance();
		$out = array();
		if($oper['type'] == 'modify.sell' || $oper['type'] == 'modify.buy')
		{
			$modify_type = $oper['type'] == 'modify.sell' ? 'modify.buy' : 'modify.sell';
			$out = $CI->MOper->getFirstRow(array('parent_id' => $oper['parent_id'], 'type' => $modify_type));
		}
		return $out;
	}

	public function addNgBonus($oper, $summ = -1)
	{
		if($oper['date'] >= NG_BONUS_BEGIN && $oper['date'] <= NG_BONUS_END && $oper['date'] != NG_BONUS_NO)
		{
			$percent_ng = 25;
			if($oper['date'] >= NG_BONUS_BEGIN2 && $oper['date'] <= NG_BONUS_END2)
			{
				$percent_ng = 30;
			}
			$this->_addNgPatriotArestBonus($oper, $summ, $percent_ng, 'ng', 'новогодний бонус '.$percent_ng.'%');
		}
	}

	public function addPatriotBonus($oper, $summ = -1)
	{
		if($oper['date'] >= PATRIOT_BONUS_BEGIN && $oper['date'] <= AREST_BONUS_END)
		{
			$this->_addNgPatriotArestBonus($oper, $summ, 25, 'patriot', 'патриотический бонус 25%');
		}
	}

	public function addArestBonus($oper, $summ = -1)
	{
		if($oper['date'] >= AREST_BONUS_BEGIN && $oper['date'] <= AREST_BONUS_END)
		{
			$this->_addNgPatriotArestBonus($oper, $summ, 15, 'arest', 'победный бонус 15%');
		}
	}

	public function add8MartaBonus($oper, $summ = -1)
	{
		if($oper['date'] >= MART8_BONUS_BEGIN && $oper['date'] <= MART8_BONUS_END)
		{
			$this->_addNgPatriotArestBonus($oper, $summ, 5, '8marta', 'бонус 8 марта 5%');
		}
	}

	private function _addNgPatriotArestBonus($oper, $summ, $percent_bonus, $type, $hranen)
	{
		global $VALUTA;
		if($summ < 0)
		{
			$summ = $oper['summ'];
		}
		if($summ > 0)
		{
			$CI = &get_instance();
			$summ_bonus = round(getFloat($summ) * $percent_bonus / 100, 4);
			$save = array(
				'parent_id' => $oper['id'],
				'user_id' => $oper['user_id'],
				'date' => $oper['date'],
				'type' => $type,
				'bonus' => 1,
				'summ' => $summ_bonus,
				'valuta' => $oper['valuta'],
				'kurs' => $oper['kurs'],
				'kurs_valuta' => $oper['kurs_valuta'],
				'percent' => $oper['percent'],
				'depo' => $oper['depo'],
				'mavro_valuta' => $oper['mavro_valuta'],
				'hranen' => $hranen,
			);
			$kurs_valuta = $oper['valuta'] == $oper['mavro_valuta'] ? 1 : $oper['kurs_valuta'];
			$kurs_valuta_real = $CI->MKurs->getExchange($oper['date'], $VALUTA[$oper['valuta']], $VALUTA[$oper['mavro_valuta']]);
			$is_reverse = $kurs_valuta_real > 1 && $kurs_valuta < 1 || $kurs_valuta_real < 1 && $kurs_valuta > 1;
			$save['mavro'] = $is_reverse ? $save['summ'] / $save['kurs_valuta'] / $save['kurs'] : $save['summ'] * $save['kurs_valuta'] / $save['kurs'];
			$save['mavro'] = round($save['mavro'], 4);
			$save['ostatok'] = $save['mavro'];
			$id = 0;
			$CI->MOper->save($save, $id);
		}
	}

	public function addRefBonus($oper, $ref_user, $user, $summ = -1, $mavro_valuta = null, $depo_type = null, $sverh = null)
	{
		global $VALUTA;
		$CI = &get_instance();
		if($summ < 0)
		{
			$summ = $oper['summ'];
		}
		$mavro_valuta = $mavro_valuta ? $mavro_valuta : $oper['valuta'];
		$depo_type = $depo_type ? $depo_type : 'base';
		$percent = getPercentByType($depo_type, $sverh, $ref_user, $oper['date']);
		$ref_kurs = $CI->MKurs->getKurs($oper['date'], $percent, 'buy');
		$ref_bonus = $CI->MOper->getCount(array('bonus' => 0, 'user_id' => $oper['user_id'], 'type' => array('buy'), 'date<' => $oper['date'])) ? 10 : 20;
		$user_is_lgota = $CI->MUser->isLgota($ref_user, $oper['date'], $depo_type);
		$depo = getDepoMonth($user_is_lgota, $percent);
		$save = array(
			'parent_id' => $oper['id'],
			'user_id' => $ref_user['id'],
			'date' => $oper['date'],
			'type' => 'ref',
			'bonus' => 1,
			'summ' => round($summ * $ref_bonus / 100, 4),
			'valuta' => $oper['valuta'],
			'kurs' => $ref_kurs,
			'percent' => $percent,
			'depo' => $depo,
			'mavro_valuta' => $mavro_valuta,
			'hranen' => 'реф бонус ('.$ref_bonus.'%) '.$user['fio'].' ('.$user['email'].')',
		);

		$save['kurs_valuta'] = $save['mavro_valuta'] == $save['valuta'] ? 1 : $CI->MKurs->getExchange($save['date'], $VALUTA[$save['valuta']], $VALUTA[$save['mavro_valuta']]);
		$save['kurs_valuta'] = $save['kurs_valuta'] > 0 ? $save['kurs_valuta'] : 1;
		$save['mavro'] = $save['summ'] * $save['kurs_valuta'] / $save['kurs'];
		$save['mavro'] = round($save['mavro'], 4);
		$save['ostatok'] = $save['mavro'];
		$id_ref = 0;
		$CI->MOper->save($save, $id_ref);
	}

	public function getRoot($oper)
	{
		$CI = &get_instance();
		$root = $oper;
		while(@$root['parent_id'])
		{
			$new_root = $CI->MOper->get($root['parent_id']);
			if(@$new_root['id'])
			{
				$root = $new_root;
			}
			else
			{
				break;
			}
		}
		return $root;
	}

	public function addSellFire($root_oper, $date, $type)
	{
		$CI = &get_instance();
		$parent_oper = $CI->MOper->getFirstRow(array('type' => $type, 'parent_id' => $root_oper['id']));
		if(@$parent_oper['id'])
		{
			$kurs_fire = $CI->MKurs->getKurs($date, $parent_oper['percent'], 'sell');
			$summ_fire = round($parent_oper['mavro'] * $kurs_fire, 4);
			$type = 'sell.fire.'.$type;
			$hranen = "Сгорание ".$parent_oper['hranen']." ".getPrintDate($parent_oper['date']);
			$save_fire = array(
				'parent_id' => $parent_oper['id'],
				'user_id' => $parent_oper['user_id'],
				'date' => $date,
				'type' => $type,
				'bonus' => 1,
				'summ' => $summ_fire,
				'valuta' => $parent_oper['mavro_valuta'],
				'kurs' => $kurs_fire,
				'kurs_valuta' => 1,
				'percent' => $parent_oper['percent'],
				'depo' => $parent_oper['depo'],
				'mavro' => $parent_oper['mavro'],
				'mavro_valuta' => $parent_oper['mavro_valuta'],
				'hranen' => $hranen,
				'dop' => 'Досрочная продажа вклада '.getPrintDate($root_oper['date']),
			);
			$id = 0;
			$CI->MOper->save($save_fire, $id);

			$arr_ostatok = array('ostatok' => 0);
			$CI->MOper->save($arr_ostatok, $parent_oper['id']);
			$parent_oper['ostatok'] = $arr_ostatok['ostatok'];
			operUpdateFrozen($parent_oper);
		}
	}


}